package com.tianpengtech.common.util.push;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class Jpush {

	private static final Logger log =Logger.getLogger(Jpush.class);
	private static String masterSecret = "16fa00649bab368bca6754b3";
	private static String appKey = "6a81d135389c221b23441e27";
	
	public final static String PUSH_TASK="task";
	public final static String PUSH_MSG="msg";
	
	public final static int LIVE_TIME=90*1000;
	public final static boolean PRO_ENV=false;
	
	
	public static void pushCustomContentToSomeOne(String alias,String ALERT,String type){
        PushResult result = jPushCustomContentToSomeOne(String.valueOf(alias),ALERT,type);
        if(result != null && result.isResultOK()){
            log.info("针对别名" + alias + "的信息推送成功！");
        }else{
            log.info("针对别名" + alias + "的信息推送失败！");
        }
    }
	
	public static void pushCustomContentToAll(String ALERT,String type){
        PushResult result = jPushCustomContentToAll(ALERT,type);
        if(result != null && result.isResultOK()){
            log.info("信息推送成功！");
        }else{
            log.info("信息推送失败！");
        }
    }
	
	private static PushPayload customContentPushToAll(String title,String type){
		Map<String,String> param = new HashMap<String,String>();
		param.put("type", type);
		return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setMessage(Message.newBuilder().addExtras(param).setMsgContent(title).build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(PRO_ENV)//true-推送生产环境 false-推送开发环境（测试使用参数）
                        .setTimeToLive(LIVE_TIME)//消息在JPush服务器的失效时间（测试使用参数）
                        .build())
                .build();
		 
	}
	
	private static PushPayload customContentPushToSomeOne(String title,String someone,String pushType){
		Map<String,String> param = new HashMap<String,String>();
		param.put("type", pushType);
		return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(someone))
                .setMessage(Message.newBuilder().addExtras(param).setMsgContent(title).build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(PRO_ENV)//true-推送生产环境 false-推送开发环境（测试使用参数）
                        .setTimeToLive(LIVE_TIME)//消息在JPush服务器的失效时间（测试使用参数）
                        .build())
                .build();
		 
	}
	
	private static PushResult jPushCustomContentToSomeOne(String alias,String alert,String type){
        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
        PushPayload payload = customContentPushToSomeOne(alert,alias,type);
        try {
            return jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
            return null;
        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Code: " + e.getErrorCode());
            log.info("Error Message: " + e.getErrorMessage());
            log.info("Msg ID: " + e.getMsgId());
            return null;
        }    
    }
	
	private static PushResult jPushCustomContentToAll(String title,String type){
        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
        PushPayload payload = customContentPushToAll(title,type);
        try {
            return jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
            return null;
        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Code: " + e.getErrorCode());
            log.info("Error Message: " + e.getErrorMessage());
            log.info("Msg ID: " + e.getMsgId());
            return null;
        }    
    }
	
	public static void main(String[] args) {
		Jpush.pushCustomContentToAll("xxxxqqq！！",Jpush.PUSH_TASK);
		Jpush.pushCustomContentToSomeOne("13262535273","你的消息····来了123ddd!23223!!",Jpush.PUSH_MSG);
	}
}
